<?php
class AccountsController extends AppController {

	var $name = 'Accounts';
	
/**
 *  */
	
	function beforeFilter() {
		parent::beforeFilter();	
		$this->Auth->autoRedirect = false;
		}			
	function login (){
		//
		if ($this->Auth->login()){			
		if ($this->Auth->user()) {
            if (!empty($this->data['Account']['remember_me'])) {
                $cookie = array();
                $cookie['username'] = $this->data['Account']['username'];
                $cookie['password'] = $this->data['Account']['password'];                
                $this->Cookie->write('Auth.Account', $cookie, true, '+2 weeks');
                unset($this->data['Account']['remember_me']);
            }
            //$this->redirect($this->Auth->redirect());
        }
        if (empty($this->data)) {
            $cookie = $this->Cookie->read('Auth.Account');
            if (!is_null($cookie)) {
                if ($this->Auth->login($cookie)) {
                    //  Clear auth message, just in case we use it.
                    $this->Session->delete('Message.auth');
                    //$this->redirect($this->Auth->redirect());
                }
            }
        }
		}
		// redirect
		$account=$this->Auth->user();
		$role=$account['Account']['role'];
		if ($role=='admin')    $this->redirect(array ('controller'=>'admin','action'=>'home'));
		if ($role=='manager')  $this->redirect(array ('controller'=>'managers','action'=>'home'));
		if ($role=='teacher')  $this->redirect(array ('controller'=>'teachers','action'=>'home'));
	}
	function logout (){
		$this->redirect($this->Auth->logout());
	}
	function register() {
		if(!empty($this->data)) {
			if($this->Account->save($this->data)) {
				// send signup email containing password to the Account
				$this->redirect('login');
			}
		}
	}
}
